package com.kxn.multi.module.service.service.impl;

import com.kxn.multi.module.service.entity.AdminRolePermission;
import com.kxn.multi.module.service.mapper.AdminRolePermissionMapper;
import com.kxn.multi.module.service.service.AdminRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kxn.multi.module.service.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import com.kxn.multi.module.common.exception.ServiceException;

/**
* <p>
* 角色权限表 服务实现类
* </p>
*
* @author kxn
* @since 2023-04-18
*/
@Service
public class AdminRolePermissionServiceImpl extends ServiceImpl<AdminRolePermissionMapper, AdminRolePermission> implements AdminRolePermissionService {

    @Autowired
    private PageUtil pageUtil;

    /**
    * 角色权限表分页列表
    * @param param 根据需要进行传值
    * @return
    */
    @Override
    public IPage<AdminRolePermission> page(AdminRolePermission param) {

        QueryWrapper<AdminRolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
        // 角色id
                .eq(param.getRoleId() != null, AdminRolePermission::getRoleId, param.getRoleId())
            // 权限id
                .eq(param.getPermissionId() != null, AdminRolePermission::getPermissionId, param.getPermissionId())
    ;

        IPage<AdminRolePermission> page = page(pageUtil.page(), queryWrapper);
        return page;
    }

    /**
    * 角色权限表详情
    * @param id
    * @return
    */
    @Override
    public AdminRolePermission detail(Long id) {
        return getById(id);
    }

    /**
    * 角色权限表新增
    * @param param 根据需要进行传值
    * @return
    */
    @Override
    public void create(AdminRolePermission param) {
        if (!save(param)) {
            throw new ServiceException("添加数据失败");
        }
    }

    /**
    * 角色权限表修改
    * @param param 根据需要进行传值
    * @return
    */
    @Override
    public void update(AdminRolePermission param) {
        if (!updateById(param)) {
            throw new ServiceException("更新数据失败");
        }
    }

    /**
    * 角色权限表修改
    * @param id 主键id
    * @param param 根据需要进行传值
    * @return
    */
    public void updateById(Long id, AdminRolePermission param) {
        AdminRolePermission data = this.detail(id);
        if (data == null) {
            throw new ServiceException("数据不存在");
        }
        param.setId(id);
        update(param);
    }

    /**
    * 角色权限表删除(单个条目)
    * @param id
    * @return
    */
    @Override
    public void delete(Long id) {
        removeById(id);
    }

    /**
    * 角色权限表删除(多个条目)
    * @param ids
    * @return
    */
    @Override
    public void batchDelete(List<Long> ids) {
        removeByIds(ids);
    }
}
